const express=require("express");
const handleDB=require("../db/handleDB");
const common=require("../utils/common");
const constant=require("../utils/constant");
const md5=require("md5");
const Captcha=require("../utils/captcha");
const keys = require("../utils/keys");
const jwt=require("jsonwebtoken");
require("../utils/filter");
const multer=require("multer");

const storage=multer.diskStorage({
    destination:function(req,file,cb){
        cb(null,'public/upload/avatar')
    },
    filename:function(req,file,cb){
        cb(null,file.originalname)
    }
})
const upload=multer({storage:storage})

const router=express.Router();

router.get("/merchant_administer/",(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);
        let data={
            user_info:userInfo[0]?{
                nick_name:userInfo[0].nick_name,
                head_url:userInfo[0].head_url?userInfo[0].head_url:"/images/worm.jpg",
                abstract:userInfo[0].abstract
            }:false
        };
        res.render("./user_index",data);
    })();
})

router.all("/user_index_basic_info/",(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);
        if(req.method==="GET"){
            let data={
                nick_name:userInfo[0].nick_name,
                phone:userInfo[0].phone,
                mail:userInfo[0].mail,
                abstract2:userInfo[0].abstract
            }
            res.render("./user_index_basic_info",data)
        }else if(req.method==="POST"){
            let {nick_name,phone,mail,abstract2}=req.body
            if(!nick_name || !phone || !mail || !abstract2){
                res.send({errmsg:"参数错误"})
                return
            }

            await handleDB(res,"user","update","数据库更新数据出错",`user_id=${userInfo[0].user_id}`,{
                nick_name,
                phone,
                mail,
                abstract:abstract2
            })

            res.send({errno:"0",errmsg:"操作成功"})
        }
    })();
})

router.get("/user_index_commodity_management/",(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);
        let commodityResult=await handleDB(res,"commodity","find","数据库查询出错",`merchant_id=${userInfo[0].user_id}`)
        for(var i=0;i<commodityResult.length;i++){
            let classify=await handleDB(res,"classify","find","数据库查询出错",`classify_id=${commodityResult[i].classify_id}`)
            commodityResult[i].commenter={
                classify_name:classify[0].classify_name
            }
        }
        if(commodityResult[0]==undefined){
            commodityResult=0;
        }
        let data={
            commodityResult
        }
        res.render("./user_index_commodity_management",data);
    })();
})

router.get("/user_index_commodity_management/modify/:commodity_id",(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);

        let {commodity_id}=req.params;
        let commodityResult=await handleDB(res,"commodity","find","数据库查询出错",`commodity_id=${commodity_id} and merchant_id=${userInfo[0].user_id}`)
        
        let classify=await handleDB(res,"classify","find","数据库查询出错",`1 order by classify_id`);
        let data={
            commodity_name:commodityResult[0].commodity_name,
            classify_id:commodityResult[0].classify_id,
            classify,
            unit_price:commodityResult[0].unit_price,
            abstract2:commodityResult[0].abstract,
            commodity_url:commodityResult[0].commodity_url,
            commodity_id,
            merchant_id:commodityResult[0].merchant_id
        }
        res.render("./user_index_commodity_management_modify",data);
    })();
})

router.post("/user_index_commodity_management/modity/update",upload.single("avatar_img"),(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);

        let file=req.file
        let {commodity_name,classify_id,unit_price,abstract,merchant_id,commodity_id}=req.body;
        if(!commodity_name || !classify_id || !unit_price || !abstract){
            res.send({errmsg:"参数错误1"})
            return
        }
        if(!file){
            await handleDB(res,"commodity","update","数据库更新数据出错",`merchant_id=${merchant_id} and commodity_id=${commodity_id}`,{
                commodity_name,
                classify_id,
                unit_price,
                abstract,
                upload_time:new Date().toLocaleString('chinese',{hour12:false})
            })
        }
        else{
            await handleDB(res,"commodity","update","数据库更新数据出错",`merchant_id=${merchant_id} and commodity_id=${commodity_id}`,{
                commodity_name,
                classify_id,
                unit_price,
                abstract,
                commodity_url:constant.QINIU_AVATAR_URL_PRE+file.originalname,
                upload_time:new Date().toLocaleString('chinese',{hour12:false})
            })
        }
        res.send({errno:"0",errmsg:"操作成功"});
    })();
})

router.post("/user_index_commodity_management/del",(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);

        let {commodity_id,merchant_id}=req.body;
        if(!commodity_id || !merchant_id){
            res.send({errmsg:"参数错误1"})
            return
        }
        await handleDB(res,"commodity","delete","数据库删除失败",`commodity_id=${commodity_id} and merchant_id=${merchant_id}`)
        await handleDB(res,"collect","delete","数据库删除失败",`commodity_id=${commodity_id}`)
        await handleDB(res,"indent","delete","数据库删除失败",`commodity_id=${commodity_id}`)
        await handleDB(res,"comment","delete","数据库删除失败",`commodity_id=${commodity_id}`)
        res.send({errno:"0",errmsg:"操作成功"});
    })();
})

router.get("/user_index_commodity_management_release/",(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);
        let classify=await handleDB(res,"classify","find","数据库查询出错",`1 order by classify_id`);
        let data={
            classify
        }
        res.render("./user_index_commodity_management_release",data);
    })();
})

router.post("/user_index_commodity_management_release/update",upload.single("avatar"),(req,res)=>{
    (async function(){
        let userInfo=await common.getUserindexInfo(req,res);

        let file=req.file
        let {commodity_name,classify_id,unit_price,abstract}=req.body;
        if(!commodity_name || !classify_id || !unit_price || !abstract){
            res.send({errmsg:"参数错误1"})
            return
        }
        await handleDB(res,"commodity","insert","数据库更新数据出错",{
            commodity_name,
            classify_id,
            unit_price,
            abstract,
            commodity_url:constant.QINIU_AVATAR_URL_PRE+file.originalname,
            merchant_id:userInfo[0].user_id,
            sales_volume:0,
            upload_time:new Date().toLocaleString('chinese',{hour12:false})
        })
        res.send({errno:"0",errmsg:"操作成功"});
    })();
})

router.post("/user_index_user_url/update",upload.single("user_url"),(req,res)=>{
    (async function(){
        let userInfo=await common.getUserInfo(req,res);

        let file=req.file
        if(file){
            await handleDB(res,"user","update","数据库更新数据出错",`user_id=${userInfo[0].user_id}`,{
                head_url:constant.QINIU_AVATAR_URL_PRE+file.originalname
            })
        }
        else{
        }
        res.send({errno:"0",errmsg:"操作成功"});
    })();
})

router.get("/user_index_commodity_indent/",(req,res)=>{
    (async function(){
        let userInfo=await common.getUserInfo(req,res);
        let indent=await handleDB(res,"indent","find","数据库查询出错",`merchant_id=${userInfo[0].user_id} and is_order=1`)

        if(indent[0]==undefined){
            indent=0;
        }
        for(var i=0;i<indent.length;i++){
            let user=await handleDB(res,"user","find","数据库查询出错",`user_id=${indent[i].user_id}`)
            let commodity=await handleDB(res,"commodity","find","数据库查询出错",`commodity_id=${indent[i].commodity_id}`)
            let address=await handleDB(res,"receiving_address","find","数据库查询出错",`address_id=${indent[i].address_id}`)
            indent[i].nick_name=user[0].nick_name
            indent[i].commodity_name=commodity[0].commodity_name
            indent[i].phone=address[0].phone
            indent[i].address=address[0].address
        }
        let data={
            indent
        }
        res.render("./web_user_commodity_indent",data);
    })();
})

module.exports=router